import {
  queryResult,
  queryOptLog,
  editResult,
  adjustByGmv,
  getById,
  verifyResult,
  sendForecastSign,
  updateToVerification,
  turnoverToSale,
  manualPass,
  batchPassCheck,
  handBatchEdit,
  queryForecastProgress,
} from '@/services/forecastResult';

import { listCity, listWarehouse } from '@/services/forecastDimension';
import {
  fetchProgress,
  updateProgressStatusByParams,
  sendInstructionByParams,
} from '@/services/forecastProgress';

export default {
  namespace: 'forecastResult',
  state: {
    data: {},
  },
  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const resp = yield call(queryResult, payload);
      const { state, data = [] } = resp;
      yield put({
        type: 'save',
        payload: data,
      });
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *fetchOptLog({ payload, callback }, { call }) {
      const resp = yield call(queryOptLog, payload);
      const { state, data = [] } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *fetchForecastProgress({ payload, callback }, { call }) {
      const resp = yield call(queryForecastProgress, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *update({ payload, callback }, { call }) {
      const resp = yield call(editResult, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *getById({ payload, callback }, { call }) {
      const resp = yield call(getById, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *adjustByGmv({ payload, callback }, { call }) {
      const resp = yield call(adjustByGmv, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *verifyResult({ payload, callback }, { call }) {
      const resp = yield call(verifyResult, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *turnoverToSale({ payload, callback }, { call }) {
      const resp = yield call(turnoverToSale, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *sendForecastSign({ payload, callback }, { call }) {
      const resp = yield call(sendForecastSign, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *updateToVerification({ payload, callback }, { call }) {
      const resp = yield call(updateToVerification, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *handPassCheck({ payload, callback }, { call }) {
      const resp = yield call(manualPass, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *handBatchPassCheck({ payload, callback }, { call }) {
      const resp = yield call(batchPassCheck, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },

    *handBatchEdit({ payload, callback }, { call }) {
      const resp = yield call(handBatchEdit, payload);
      const { state } = resp;
      if (resp && state === 0 && callback) {
        callback();
      }
    },
    *listCity({ payload, callback }, { call }) {
      const resp = yield call(listCity, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *listWarehouse({ payload, callback }, { call }) {
      const resp = yield call(listWarehouse, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },

    *fetchProgress({ payload, callback }, { call }) {
      const resp = yield call(fetchProgress, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *updateProgressStatusByParams({ payload, callback }, { call }) {
      const resp = yield call(updateProgressStatusByParams, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *sendInstructionByParams({ payload, callback }, { call }) {
      const resp = yield call(sendInstructionByParams, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
